﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using WebSystem.WebControls.ExtendControl;

namespace ExtAspNet.Examples
{
    public partial class _default : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Theme & Language.
                ddlTheme.SelectedValue = PageManager1.Theme.ToString().ToLower();
                ddlLanguage.SelectedValue = PageManager1.Language.ToString().ToLower();

                // Current version.
                //labCurrentVersion.Text = ExtAspNet.GlobalConfig.ProductVersion;

                // Site source code.
                //btnSourceCode.OnClientClick = windowSourceCode.GetShowReference("./source.aspx?files=default.aspx;menu.xml;Web.config;Code/PageBase.cs");

                // Bind xml to tree.
                treeMenu.DataSource = XmlDataSource1;
                treeMenu.DataBind();
            }
        }

        protected void btnSignOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }

        #region ProcessPageQueryString

        //private void ProcessPageQueryString()
        //{
        //    string framePageUrl = Request.QueryString["page"];
        //    if (!String.IsNullOrEmpty(framePageUrl))
        //    {
        //        TreeNode node = FindTreeNode(treeMenu.Nodes, null, ResolveUrl(framePageUrl).ToLower());
        //        if (node != null)
        //        {
        //            treeMenu.SelectedNode = node;
        //            ExpandParentTreeNode(node);
        //            mainRegion.IFrameUrl = framePageUrl;
        //        }
        //    }
        //}

        //private void ExpandParentTreeNode(TreeNode node)
        //{
        //    if (parentLinks.ContainsKey(node))
        //    {
        //        TreeNode parentNode = parentLinks[node];
        //        parentNode.Expanded = true;
        //        ExpandParentTreeNode(parentNode);
        //    }
        //}

        //private Dictionary<TreeNode, TreeNode> parentLinks = new Dictionary<TreeNode, TreeNode>();
        //private TreeNode FindTreeNode(TreeNodeCollection nodes, TreeNode parentNode, string url)
        //{
        //    if (nodes.Count == 0)
        //    {
        //        return null;
        //    }

        //    foreach (TreeNode node in nodes)
        //    {
        //        // Add links between current node and parent node.
        //        if (parentNode != null)
        //        {
        //            parentLinks.Add(node, parentNode);
        //        }

        //        if (node.Leaf && !String.IsNullOrEmpty(node.NavigateUrl))
        //        {
        //            if (ResolveUrl(node.NavigateUrl).ToLower() == url)
        //            {
        //                return node;
        //            }
        //        }

        //        if (node.Nodes.Count > 0)
        //        {
        //            TreeNode childNode = FindTreeNode(node.Nodes, node, url);
        //            if (childNode != null)
        //            {
        //                return childNode;
        //            }
        //        }
        //    }

        //    return null;
        //}



        #endregion

        /// <summary>
        /// Change theme.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpCookie themeCookie = new HttpCookie("Theme", ddlTheme.SelectedValue);
            themeCookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(themeCookie);

            PageContext.Refresh();
        }


        protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpCookie langCookie = new HttpCookie("Language", ddlLanguage.SelectedValue);
            langCookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(langCookie);

            PageContext.Refresh();
        }

    }
}
